package cc.lj.internal.payment.domain.settlement.service.impl;

import cc.lj.internal.payment.domain.pay.service.ITicketPayRecordDomainService;
import cc.lj.internal.payment.domain.pay.vo.CouponsSettlementDO;
import cc.lj.internal.payment.domain.pay.vo.TicketPayRecordDO;
import cc.lj.internal.payment.domain.settlement.service.ITicketSettlementDomainService;
import lj.internal.dict.enums.EnumTicketPayRecordIsNewStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 收款后电子券结算　domain service 实现
 * @author liangxifeng
 * @date 2022-02-13
 */
@Service("ticketSettlementDomainServiceImpl")
@Slf4j
public class TicketSettlementDomainServiceImpl implements ITicketSettlementDomainService {
    //合同收券表 domain
    @Resource(name = "ticketPayRecordDomainServiceImpl")
    private ITicketPayRecordDomainService recordDomainService;

    @Override
    public TicketPayRecordDO doMain(CouponsSettlementDO couponsSettlementDO, BigDecimal martSum, BigDecimal batchMartSum) {
        //原卖场分摊+已收款卖场总分摊
        BigDecimal totalShare = couponsSettlementDO.getMartOldSharePrice().add(martSum);
        //log.info("原卖场分摊金额="+couponsSettlementDO.getMartOldSharePrice().floatValue()+",已收款总卖场分摊金额="+martSum+",之和="+totalShare.floatValue()+",该商户："+couponsSettlementDO.getMerchantShow()+",封顶额="+couponsSettlementDO.getCapValue());
        /**
         * 商户总分摊金额达到卖场封顶额情况：原卖场分摊+已收款卖场总分摊 >商户本期活动封顶额
        if( totalShare.floatValue() > couponsSettlementDO.getCapValue()) {
            //达到卖场封顶额后，如果使用新分摊
            if(couponsSettlementDO.getIsNewShare() == EnumTicketPayRecordIsNewStatus.USE_NEW.getCode()) {
                log.info("++大封顶额达到，使用新分摊++");
                return updateNewShare(couponsSettlementDO);
            }else {
                log.info("++大封顶额达到，使用老分摊++");
                //达到卖场封顶额后，如果使用老分摊
                return updateOldShare(couponsSettlementDO);
            }
        }
         */

        /**
         * 商户总分金额未达到卖场封顶额情况:判断该批次占用卖场封顶额比例情况
        //计算该批次占有卖场最高封顶额度 = 占有卖场最高封顶额比例 * 该商户本期活动封顶额
        BigDecimal batchMaxCapValue = couponsSettlementDO.getMartCapRatio().multiply(new BigDecimal(couponsSettlementDO.getCapValue()));
        log.info("商户："+couponsSettlementDO.getMerchantShow()+",批次主键："+couponsSettlementDO.getTicketBatchId()+",在该活动主键:"+couponsSettlementDO.getCapId()+",最高占用的封顶额="+batchMaxCapValue.floatValue());

        //原卖场分摊+该批次已收款卖场总分摊
        BigDecimal batchTotalShare = couponsSettlementDO.getMartOldSharePrice().add(batchMartSum);
        log.info("原卖场分摊金额="+couponsSettlementDO.getMartOldSharePrice().floatValue()+",该批次主键:"+couponsSettlementDO.getTicketBatchId()+",已收款总卖场分摊金额="+batchMartSum+",之和="+batchTotalShare.floatValue()+",商户："+couponsSettlementDO.getMerchantShow());

        //达到最高封顶额情况：原卖场分摊+该批次已收款卖场总分摊 > 该批次占有卖场最高封顶额
        if( batchTotalShare.floatValue() > batchMaxCapValue.floatValue()) {
            //达到卖场封顶额后，如果使用新分摊
            if(couponsSettlementDO.getIsNewShare() == EnumTicketPayRecordIsNewStatus.USE_NEW.getCode()) {
                log.info("++小封顶额达到，使用新分摊++");
                return updateNewShare(couponsSettlementDO);
            }else {
                log.info("++小封顶额达到，使用老分摊++");
                //达到卖场封顶额后，如果使用老分摊
                return updateOldShare(couponsSettlementDO);
            }
        }else {
            log.info("++大封顶额和小封顶额都未达到，使用老分摊++");
            //未卖场封顶额使用老分摊
            return updateOldShare(couponsSettlementDO);
        }
         */
        return null;
    }

    /**
     * 修改原分摊数据
     * @param couponsSettlementDO
     * @return 最终修改的合同收券表数据DO
     */
    private TicketPayRecordDO updateOldShare(CouponsSettlementDO couponsSettlementDO) {
        TicketPayRecordDO ticketPayRecordDO = new TicketPayRecordDO();
        /*
        ticketPayRecordDO.setId(couponsSettlementDO.getTicketPayRecordId());
        ticketPayRecordDO.setCapId(couponsSettlementDO.getCapId());
        ticketPayRecordDO.setIsNewShare(EnumTicketPayRecordIsNewStatus.USE_OLD.getCode());
        ticketPayRecordDO.setMartShareRatio(couponsSettlementDO.getMartOldShareRatio());
        ticketPayRecordDO.setMartSharePrice(couponsSettlementDO.getMartOldSharePrice());
        ticketPayRecordDO.setMerchantSharePrice(couponsSettlementDO.getMerchantOldSharePrice());
        ticketPayRecordDO.setMerchantShareRatio(couponsSettlementDO.getMerchantOldShareRatio());
        ticketPayRecordDO.setMerchantShow(couponsSettlementDO.getMerchantShow());
        log.info("商户："+couponsSettlementDO.getMerchantShow()+",批次主键："+couponsSettlementDO.getTicketBatchId()+",=使用老分摊结算,结算数据="+ticketPayRecordDO);
        recordDomainService.updateById(ticketPayRecordDO);*/
        return ticketPayRecordDO;
    }
    /**
     * 修改新分摊数据
     * @param couponsSettlementDO
     * @return 最终修改的合同收券表数据DO
     */
    private TicketPayRecordDO updateNewShare(CouponsSettlementDO couponsSettlementDO) {
        TicketPayRecordDO ticketPayRecordDO = new TicketPayRecordDO();
        /*
        ticketPayRecordDO.setId(couponsSettlementDO.getTicketPayRecordId());
        ticketPayRecordDO.setCapId(couponsSettlementDO.getCapId());
        ticketPayRecordDO.setIsNewShare(EnumTicketPayRecordIsNewStatus.USE_NEW.getCode());
        ticketPayRecordDO.setMartShareRatio(couponsSettlementDO.getMartNewShareRatio());
        ticketPayRecordDO.setMartSharePrice(couponsSettlementDO.getMartNewSharePrice());
        ticketPayRecordDO.setMerchantSharePrice(couponsSettlementDO.getMerchantNewSharePrice());
        ticketPayRecordDO.setMerchantShareRatio(couponsSettlementDO.getMerchantNewShareRatio());
        ticketPayRecordDO.setMerchantShow(couponsSettlementDO.getMerchantShow());
        log.info("商户："+couponsSettlementDO.getMerchantShow()+",批次主键："+couponsSettlementDO.getTicketBatchId()+",=使用新分摊结算,结算数据="+ticketPayRecordDO);
        recordDomainService.updateById(ticketPayRecordDO);*/
        return ticketPayRecordDO;
    }

}
